#!/bin/bash

BOCHS=$1
if [ -z "${BOCHS}" ]; then
    BOCHS="bochs-2.7"
fi

SRC_ROOT=`pwd`

if [ ! -d ${SRC_ROOT}/external ]; then
    mkdir ${SRC_ROOT}/external
fi

if [ ! -d ${SRC_ROOT}/external/${BOCHS} ]; then
    tar -zxvf ${SRC_ROOT}/downloads/${BOCHS}.tar.gz -C ${SRC_ROOT}/external
fi

cd ${SRC_ROOT}/external/${BOCHS}
./configure \
        --enable-debugger \
        --enable-iodebug \
        --enable-x86-debugger \
        --with-x \
        --with-x11 \
        --enable-smp \
        --enable-x86-64 \
        --enable-sb16 \
        --enable-es1370
NRPROC=$(cat /proc/cpuinfo | grep processor | wc -l)
if [ ${NRPROC} -gt 2 ]; then
    NRPROC=`expr ${NRPROC} - 2`
fi
make -j${NRPROC}
sudo make install